package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/25 20:34
 */
public class C11 {

    // 暴力解
    public static int maxArea(int[] height) {
        int max = 0;
        for (int i = 0; i < height.length - 1; i++) {
            for (int j = i + 1; j < height.length; j++) {
                max = Math.max(max, (j - i) * Math.min(height[i], height[j]));
            }
        }
        return max;
    }

    // 双指针, O(N), 从两侧算, 每次减少低的那个, l==r退出循环
    public static int maxArea2(int[] height) {
        int l = 0, r = height.length - 1;
        int max = 0;
        while (l < r) {
            max = Math.max(max, (r - l) * Math.min(height[l], height[r]));
            if(height[l] < height[r]) {
                l++;
            }else {
                r--;
            }
        }
        return max;
    }

    // for test
    public static void main(String[] args) {
        int[] height = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        System.out.println(maxArea(height));
        System.out.println(maxArea2(height));
    }

}
